//
//  JGDealDBTool.m
//  JGMeiTuanHD
//
//  Created by JJetGu on 15/9/28.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGDealDBTool.h"
#import "FMDB.h"
#import "JGDeal.h"

@implementation JGDealDBTool

static FMDatabase *_db;

+(void)initialize
{
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"deal.sqlite"];
    
    // 1.打开数据库
    _db = [FMDatabase databaseWithPath:file];
    if (![_db open]) return;
    
    // 2.创表
    //2.1 收藏表格
    //blob 对象
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_collect_deal(id integer PRIMARY KEY, deal blob NOT NULL, deal_id text NOT NULL);"];
    
    //2.2 最近表格
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_recent_deal(id integer PRIMARY KEY, deal blob NOT NULL, deal_id text NOT NULL);"];
    
}


+(NSArray *)collectDeals:(int)page
{
    int size = 20;
    int pos = (page - 1) * size; // 从哪里开始
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT * FROM t_collect_deal ORDER BY id DESC LIMIT %d,%d;", pos, size];
    NSMutableArray *deals = [NSMutableArray array];
    
    while (set.next) {
        JGDeal *deal = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"deal"]];
         [deals addObject:deal];
    }
    
     return deals;
}

+(void)addCollectDeal:(JGDeal *)deal
{
    //对象要归档变成data才能存入数据库
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:deal];
    [_db executeUpdateWithFormat:@"INSERT INTO t_collect_deal(deal, deal_id) VALUES(%@, %@);", data, deal.deal_id];
}

+(void)removeCollectDeal:(JGDeal *)deal
{
    [_db executeUpdateWithFormat:@"DELETE FROM t_collect_deal WHERE deal_id = %@;", deal.deal_id];
}

+(BOOL)isCollected:(JGDeal *)deal
{
    //返回个数
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT count(*) AS deal_count FROM t_collect_deal WHERE deal_id = %@;", deal.deal_id];
    [set next];
    
    return [set intForColumn:@"deal_count"] == 1;
}

+(int)collectDealsCount
{
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT count(*) AS deal_count FROM t_collect_deal;"];
    [set next];
    
    return [set intForColumn:@"deal_count"];
}


//-----------------最近-------------------
+(NSArray *)recentDeals:(int)page
{
    int size = 20;
    int pos = (page - 1) * size; // 从哪里开始
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT * FROM t_recent_deal ORDER BY id DESC LIMIT %d,%d;", pos, size];
    NSMutableArray *deals = [NSMutableArray array];
    
    while (set.next) {
        JGDeal *deal = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"deal"]];
        [deals addObject:deal];
    }
    
    return deals;
}

+(void)addRecentDeal:(JGDeal *)deal
{
    //判断是否浏览过这个团购
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT count(*) AS deal_count FROM t_recent_deal WHERE deal_id = %@;", deal.deal_id];
    [set next];
    BOOL isRecent = [set intForColumn:@"deal_count"] == 1;
    if (isRecent) { //有浏览过
        //删除
        [_db executeUpdateWithFormat:@"DELETE FROM t_recent_deal WHERE deal_id = %@;", deal.deal_id];
        //再添加
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:deal];
        [_db executeUpdateWithFormat:@"INSERT INTO t_recent_deal(deal, deal_id) VALUES(%@, %@);", data, deal.deal_id];
    } else {
        //直接添加
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:deal];
        [_db executeUpdateWithFormat:@"INSERT INTO t_recent_deal(deal, deal_id) VALUES(%@, %@);", data, deal.deal_id];
    }
}

+(void)removeRecentDeal:(JGDeal *)deal
{
    [_db executeUpdateWithFormat:@"DELETE FROM t_recent_deal WHERE deal_id = %@;", deal.deal_id];
}

+(int)recentDealsCount
{
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT count(*) AS deal_count FROM t_recent_deal;"];
    [set next];
    
    return [set intForColumn:@"deal_count"];
}


@end
